
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class VDepthOnlyPass : VScriptableRenderPass
{
    private static readonly ShaderTagId k_ShaderTagId = new ShaderTagId("VRP_DepthOnly");
    
    private RenderTargetHandle depthAttachmentHandle { get; set; }
    internal RenderTextureDescriptor descriptor { get; set; }
    internal bool allocateDepth { get; set; } = true;
    internal ShaderTagId shaderTagId { get; set; } = k_ShaderTagId;
    public VDepthOnlyPass(RenderPassEvent renderPassEvent):base(renderPassEvent)
    {

    }
    public void Setup(
        RenderTextureDescriptor baseDescriptor,
        RenderTargetHandle depthAttachmentHandle)
    {
        this.depthAttachmentHandle = depthAttachmentHandle;
        
        baseDescriptor.colorFormat = RenderTextureFormat.Depth;
        baseDescriptor.depthBufferBits = 32;
        
        // Depth-Only pass don't use MSAA
        baseDescriptor.msaaSamples = 1;
        this.descriptor = baseDescriptor;
        this.allocateDepth = true;
        this.shaderTagId = k_ShaderTagId;
    }
    public override void OnCameraSetup(CommandBuffer cmd, ref VRenderingData renderingData)
    {
        if (this.allocateDepth)
            cmd.GetTemporaryRT(depthAttachmentHandle.id, descriptor, FilterMode.Point);
        var desc = renderingData.cameraData.cameraTargetDescriptor;

       
    }
    public override void Execute(ScriptableRenderContext context, ref VRenderingData renderingData)
    {
       
        // CommandBuffer cmd = CommandBufferPool.Get();
        
        var sortingSettings = new SortingSettings(renderingData.cameraData.camera);
        var drawingSettings = CreateDrawingSettings(k_ShaderTagId, ref renderingData, SortingCriteria.BackToFront);
       
        
        var filteringSettings = new FilteringSettings(RenderQueueRange.all);
        context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings);
        Debug.Log("VDepthOnlyPass Execute");
        // context.ExecuteCommandBuffer(cmd);
        // CommandBufferPool.Release(cmd);
    }
}